package com.example.websocket_demo.dao.message;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface MessageDao {

    @Insert("<script> insert into message <trim prefix='(' suffix=')' suffixOverrides=','> <if test='send_user_id != null and send_user_id != \"\"'>send_user_id,</if> <if test='receive_type != null and receive_type != \"\"'>receive_type,</if> <if test='receive_user_id != null and receive_user_id != \"\"'>receive_user_id,</if> <if test='receive_group_id != null and receive_group_id != \"\"'>receive_group_id,</if> <if test='message_str != null and message_str != \"\"'>message_str,</if> <if test='created_time_str != null and created_time_str != \"\"'>created_time_str,</if> </trim> <trim prefix='values (' suffix=')' suffixOverrides=','> <if test='send_user_id != null and send_user_id != \"\"'> #{send_user_id}, </if> <if test='receive_type != null and receive_type != \"\"'> #{receive_type}, </if>  <if test='receive_user_id != null and receive_user_id != \"\"'> #{receive_user_id}, </if>  <if test='receive_group_id != null and receive_group_id != \"\"'> #{receive_group_id}, </if>  <if test='message_str != null and message_str != \"\"'> #{message_str}, </if> <if test='created_time_str != null and created_time_str != \"\"'> #{created_time_str}, </if> </trim> </script>")
    public int insertMessage(Map<String, Object> messageMap);

    @Delete("<script> delete * from message where id=#{id}</script>")
    public int deleteMessage(Integer id);

    @Update("<script> update message <trim prefix='set' suffixOverrides=','> <if test='send_user_id!=null'> send_user_id=#{send_user_id}, </if> <if test='receive_type!=null'> receive_type=#{receive_type}, </if> <if test='receive_user_id!=null'> receive_user_id=#{receive_user_id}, </if> <if test='receive_group_id!=null'> receive_group_id=#{receive_group_id}, </if> <if test='dr!=null'> dr=#{dr}, </if> <if test='message_str!=null'> message_str=#{message_str}, </if></trim> where id=#{id}</script>")
    public int updateMessage(Map<String, Object> messageMap);

    @Select("<script> select * from message where 1=1 <if test='id!=null'>and id=#{id} </if> <if test='send_user_id!=null'>and send_user_id=#{send_user_id} </if> <if test='receive_type!=null'>and receive_type=#{receive_type} </if> <if test='receive_user_id!=null'>and receive_user_id=#{receive_user_id} </if> <if test='receive_group_id!=null'>and receive_group_id=#{receive_group_id} </if> <if test='message_str!=null'>and message_str=#{message_str}</if></script>")
    public List<Map<String,Object>> selectMessage(Map<String, Object> messageMap);

    @Select("<script> select * from message where 1=1 <if test='receive_type!=null'>and receive_type=#{receive_type} </if> <if test='send_user_id!=null'>and (send_user_id=#{send_user_id} </if> <if test='receive_user_id!=null'>or receive_user_id=#{receive_user_id})</if></script>")
    public List<Map<String,Object>> selectMessageBySidOrRid(Map<String, Object> messageMap);

    @Select("<script> select * from message </script>")
    public List<Map<String,Object>> selectMessages();
}
